package db

import (
	"com.jiaguyu/model"

	"com.jiaguyu/logger"
	"database/sql"
	"github.com/jmoiron/sqlx"
)

func GetCategoryList() (categoryList []*model.Category, err error) {
	sqlstr := `select category_id,category_name from category `
	err = DB.Select(&categoryList, sqlstr)

	//有可能数据 结构中为空
	if err == sql.ErrNoRows {
		err = nil
		return
	}
	if err != nil {
		return
	}
	return
}

func MGetCategoryList(categoryIds []int64) (categoryMap map[int64]*model.Category, err error) {
	sqlstr := `select category_id,category_name from category where category_id in(?)`
	var categoryTempArr []interface{}
	for _, categoryId := range categoryIds {
		categoryTempArr = append(categoryTempArr, categoryId)
	}
	//转换数组
	query, args, err := sqlx.In(sqlstr, categoryTempArr...)
	if err != nil {
		logger.Error("SQLX 出问题%v、n", err)
		return
	}
	categoryMap = make(map[int64]*model.Category, len(categoryIds))
	var categoryList []*model.Category

	err = DB.Select(&categoryList, query, args...)

	//有可能数据 结构中为空
	if err == sql.ErrNoRows {
		err = nil
		return
	}
	if err != nil {
		return
	}
	for _, v := range categoryList {
		categoryMap[v.CategoryId] = v
	}
	return
}
